<?php

namespace G4T\Form\Form;

use G4T\Form\Element\TypeInterface;

class JqgridForm extends \Zend_Form
{

    /**
     * Url для получение списка.
     * @var
     */
    private $url;

    /**
     * Url для редактирования
     * @var
     */
    private $editUrl;

    /**
     * Динамечески вызывается \Zend_Form::setOptions()
     * @param $value
     */
    public function setUrl($value)
    {
        $this->url = $value;
    }

    /**
     * Динамечески вызывается \Zend_Form::setOptions()
     * @param $value
     */
    public function setEditUrl($value)
    {
        $this->editUrl = $value;
    }

    public function render()
    {
        $content = <<<_END

                    <table id="rowed3">
                        <div id="prowed3">
                        </div>
                    </table>
<script type="text/javascript">
    $().ready(function () {
        $("#rowed3").jqGrid({ url:'{$this->url}', datatype:"json", colNames:{$this->getColNames()}, colModel:{$this->getColModel()}
        , rowNum:10, rowList:[10, 20, 30],
        pager:'#prowed3', sortname:'id', viewrecords:true, sortorder:"desc", editurl:"{$this->editUrl}",
        caption:"Using navigator" });
        jQuery("#rowed3").jqGrid('navGrid', "#prowed3", {edit:true, add:true, del:true});
        jQuery("#rowed3").jqGrid('inlineNav', "#prowed3");
    });
</script>
_END;

        return $content;
    }

    private function getColNames()
    {
        $result = array();
        foreach ($this->_elements as $element) {
            if (!($element instanceof \Zend_Form_Element_Submit)) {
                $name = ucfirst($element->getName());
                $result[] = $name;
            }
        }
        return json_encode($result);
    }

    private function getColModel()
    {
        $result = array();
        foreach ($this->_elements as $element) {
            if (!($element instanceof \Zend_Form_Element_Submit)) {
                $params['name'] = $element->getName();
                $params['index'] = $element->getName();
                if (!($element instanceof \Zend_Form_Element_Hidden)) {
                    $params['editable'] = true;
                    $params['edittype'] = $this->getElementType($element);
                    $values = $this->getFormValues($element);
                    if ($values) {
                        $params['editoptions'] = $values;
                    }
                }
                $result[] = $params;
            }
        }

        return json_encode($result);
    }

    private function getFormValues(\Zend_Form_Element $element)
    {
        if ($element instanceof \Zend_Form_Element_Select) {
            $multiOptions = $element->getMultiOptions();
            if (count($multiOptions)) {
                foreach ($multiOptions as $id => $value) {
                    $values[] = "$id:$value";
                }
                $result['value'] = $values;
                return $result;
            }
        }
        return false;
    }

    private function getElementType(TypeInterface $element)
    {
        return $element->getFormElementType();
    }
}
